package com.example.martian.utils;

import net.sf.json.JSONObject;

import java.math.BigDecimal;
import java.security.MessageDigest;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

public class Utils {

    public static void main(String[] args) {
        System.out.println(getSixGenerate());
    }

    public static String getOrderNumber() {
        int x = (int) ((Math.random() * 9 + 1) * 1000000);
        String serial = x + "";
        return "MGC".concat(serial);
    }

    /**
     * 生成指定位数的随机数
     *
     * @param length
     * @return
     */
    public static String getRandom(int length) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            val += String.valueOf(random.nextInt(10));
        }
        return val;
    }

    public static String getSixGenerate() {
        char[] letters = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        boolean[] flags = new boolean[letters.length];
        char[] chs = new char[6];
        for (int i = 0; i < chs.length; i++) {
            int index;
            do {
                index = (int) (Math.random() * (letters.length));
            } while (flags[index]);// 判断生成的字符是否重复
            chs[i] = letters[index];
            flags[index] = true;
        }
        return String.valueOf(chs);
    }


    /***
     * 将字符串转化为MD5
     */
    public static String toMD5(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16)
                hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();

    }

    /**
     * 将数据进行SHA-512加密
     */
    public static String toSHA(String inStr) {
        MessageDigest sha = null;
        try {
            sha = MessageDigest.getInstance("SHA-512");
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];
        byte[] md5Bytes = sha.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16)
                hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }

    /**
     * 将MD5解析为正常字符串（需要解析两次才可以
     */
    public static String convertMD5(String inStr) {
        char[] a = inStr.toCharArray();
        for (int i = 0; i < a.length; i++) {
            a[i] = (char) (a[i] ^ 't');
        }
        String s = new String(a);
        return s;
    }

    public static String toJSON(String jsonstr, String token) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", jsonstr);
        jsonObject.put("token", token);
        return jsonObject.toString();
    }

    /**
     * 封装为JSON对象 0-正常 1-异常
     * @return
     */
    public static String toJSON(String msg, String jsonstr, String state) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("message", msg);
        jsonObject.put("data", jsonstr);
        jsonObject.put("state", state);
        return jsonObject.toString();
    }

    public static String toJSON(String msg, String jsonstr, String state, String token) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("message", msg);
        jsonObject.put("data", jsonstr);
        jsonObject.put("state", state);
        jsonObject.put("token", token);
        System.out.println(jsonObject.toString());
        return jsonObject.toString();
    }

    public static String toJSONmap() {
        Map<String, Object> map = new HashMap<>();
        return map.toString();
    }

    /**
     * 方法名:multipleEncryption-多重加密
     */
    public static String multipleEncryption(String pwd) {
        return toSHA(toSHA(toMD5(toSHA(toMD5(pwd)))));
    }

    public static Map<String, Object> map = new HashMap<>();

    public static boolean isNumeric(String str) {
        boolean isflag = false;
        String reg = "^[0-9]+(.[0-9]+)?$";
        if (str.matches(reg)) {
            if (Double.parseDouble(str) > 0.0001) {
                isflag = true;
            }
        }
        return isflag;
    }
//    public static String utilsToJson(String msg,String jsonstr,String state){
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("msg",msg);
//        jsonObject.put("date",jsonstr);
//        jsonObject.put("state",state);
//        return jsonObject.toString();
//    }

    /**
     * 把一个正整数随机拆分成count个正整数
     *
     * @param totalNum
     * @param count
     * @return
     */
    public static List<Double> randomNumber(Double totalNum, int count) {
        // 创建一个长度的数组
        List<Double> redList = new ArrayList<>();

        Double totalMoney = (totalNum);

        /*if (totalMoney < count || totalMoney < 1) {
            return redList; // 返回空的集合
        }*/

        //2. 进行随机分配
        Random rand = new Random();

        Double leftMoney = totalMoney;  // 剩余金额
        int leftCount = count;  // 剩余份数
        // 随机分配公式：1 + rand.nextInt(leftMoney / leftCount * 2);
        for (int i = 0; i < count - 1; i++) {
            Double money_ = 0D;
            if (leftMoney > 0) {
                if ((leftMoney / leftCount * 2) < 1) {
                    money_ = leftMoney;
                } else {
                    money_ = Double.valueOf(
                            1 + rand.nextInt((int)(leftMoney / leftCount * 2)));
                }

            } else {
                money_ = 0D;
            }
            redList.add(Double.parseDouble(String.valueOf(money_)));
            if (money_ > 0) {
                leftMoney -= money_;
                leftCount--;
            }

        }
        // 把剩余的最后一个放到最后一个包里
        redList.add(leftMoney);
        return redList;
    }
}
